class Solution {
    public int findKthLargest(int[] nums, int k) {
        return qSort(nums,0,nums.length-1,k);
    }
    public int qSort(int[]nums,int l,int r,int k){
        if(l==r) return nums[l];
        int key=nums[new Random().nextInt(r-l+1)+l];
        int left=l-1,right=r+1,i=l;
        while(i<right){
            if(nums[i]<key) swap(nums,++left,i++);
            else if(nums[i]==key) i++;
            else swap(nums,--right,i);
        }
        int c=r-right+1,b=right-left-1;
        if(c>=k) return qSort(nums,right,r,k); 
        else if(c+b>=k) return key;
        else return qSort(nums,l,left,k-b-c);
    }
    public void swap(int[] nums,int i,int j){
        int t=nums[i];
        nums[i]=nums[j];
        nums[j]=t;
    }
}